package com.work.studio.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 功能 添可 饼图
 */
public class TinPieChart extends View {

    private Paint arcPaint;
    private float arcStrokeWidth = 70f;
    private Paint textPaint;
    private Paint linePaint;
    private float mWidth;
    private float mHeight;
    private float radius;
    private RectF mRectF;
    private Rect mTextRect;
    private List<String> titles = new ArrayList<>();
    private List<Float> percents = new ArrayList<>();
    private List<String> colors = new ArrayList<>();
    private Context mContext;

    private float initStartAngle = -90;
    private float sumPercent;

    private static final String TAG = "TinPieChart";

    public TinPieChart(Context context) {
        this(context, null);
    }

    public TinPieChart(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public TinPieChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setStrokeWidth(arcStrokeWidth);
        arcPaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(dp2px(10));

        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(2);

        mRectF = new RectF();
        mTextRect = new Rect();

        colors = Arrays.asList("#216BDB", "#21AADB", "#21DBCF", "#21DB53", "#AADB21", "#DBB021", "#CCCCCC");

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = mWidth/3;
        setMeasuredDimension((int) mWidth, (int) (mHeight + arcStrokeWidth + mHeight/2));
        radius = mWidth/6;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRectF.set(mWidth/3, mHeight/4, mWidth*2/3, mHeight/4 + mWidth/3);
        //画圆弧
        for (int i = 0; i < percents.size(); i++) {
            if (i == 7){
                arcPaint.setColor(Color.parseColor(colors.get(6)));
                linePaint.setColor(Color.parseColor(colors.get(6)));
            } else {
                arcPaint.setColor(Color.parseColor(colors.get(i)));
                linePaint.setColor(Color.parseColor(colors.get(i)));
            }
            float curSweepAngle = percents.get(i) * 360 / sumPercent;
            float startAngle = initStartAngle + getStartAngle(i);
            canvas.drawArc(mRectF, startAngle, curSweepAngle, false, arcPaint);
            double radian = Math.toRadians(startAngle + curSweepAngle/2);
            float x = mRectF.centerX() + (float) Math.cos(radian)*(radius + arcStrokeWidth/2);
            float y = mRectF.centerY() + (float) Math.sin(radian)*(radius + arcStrokeWidth/2);
            float x1 = mRectF.centerX() +(float) Math.cos(radian)*(radius + arcStrokeWidth/2 + 30);
            float y1 = mRectF.centerY() + (float) Math.sin(radian)*(radius + arcStrokeWidth/2 + 30);
            float x2;
            float textX;
            float textY = y1 + mTextRect.height()/2;
            String textStr = titles.get(i);
            textPaint.getTextBounds(textStr, 0, textStr.length(), mTextRect);
            //没有处理数据挤在一起的情况，垃圾啊
            if (startAngle + curSweepAngle/2 >= 90){
                x2 = x1 - 30;
                textX = x2 - mTextRect.width();
            } else {
                x2 = x1 + 30;
                textX = x2;
            }
            if (i == percents.size()-1){
                x2 = x1 + 30;
                textX = x2;
            }
            canvas.drawLine(x, y, x1, y1, linePaint);
            canvas.drawLine(x1, y1, x2, y1, linePaint);
            canvas.drawText(textStr, textX , textY, textPaint);
            canvas.drawText(percents.get(i) + "%", textX , textY + mTextRect.height(), textPaint);
        }
    }

    private float getStartAngle(int i) {
        float angle = 0;
        for (int j = 0; j < i; j++) {
            angle += percents.get(j)*360/sumPercent;
        }
        return angle;
    }

    public void setData(List<String> titles, List<Float> percents){
        this.titles = titles;
        this.percents = percents;
        for (Float percent : percents) {
            sumPercent+=percent;
        }

        postInvalidate();
    }

    private int dp2px(int dp){
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

}
